﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace DataPagination
{
    /// <summary>
    /// 数据分页框架抽象类
    /// </summary>
    public abstract class PDataPage
    {
        public PDataPage()
        { 
        
        }

        public PDataPage(int _DataSourceCount, int _PageSize, int _CurrentPageIndex)
        {
            DataSourceCount = _DataSourceCount;
            PageSize = _PageSize;
            CurrentPageIndex = _CurrentPageIndex;
        }

        #region 成员
        /// <summary>
        /// URL变量集合
        /// </summary>
        public string URLQuery = string.Empty;
        /// <summary>
        /// 分页面URL地址
        /// </summary>
        public string PageURL = string.Empty;
        /// <summary>
        /// 总记录数
        /// </summary>
        public int DataSourceCount = 0;
        /// <summary>
        /// 每页记录数
        /// </summary>
        public int PageSize = 20;
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount = 0;
        /// <summary>
        /// 当前页索引
        /// </summary>
        public int CurrentPageIndex = 1;
        /// <summary>
        /// 是否首页
        /// </summary>
        public bool IsFirstPage = false;
        /// <summary>
        /// 是否尾页
        /// </summary>
        public bool IsLastPage = false;
        /// <summary>
        /// 虚拟路径头
        /// </summary>
        public string URLHead = string.Empty;

        /// <summary>
        /// 导航模板
        /// </summary>
        protected string HtmlTemplate = string.Empty;

        #endregion

        #region 属性
        
        #endregion

        #region 方法
        /// <summary>
        /// 运算有关联的数据
        /// </summary>
        protected void OperationInfo()
        {
            PageCount = (int)Math.Ceiling(Convert.ToDouble(DataSourceCount) / Convert.ToDouble(PageSize));  //总页数
                        
            if (PageCount < 1) { PageCount = 1; }   //至少一页
            if (CurrentPageIndex < 1) { CurrentPageIndex = 1; } //不能小于第一页
            if (CurrentPageIndex > PageCount) { CurrentPageIndex = PageCount; } //不能大于最后页

            IsFirstPage = (CurrentPageIndex == 1);  //是否首页
            IsLastPage = (CurrentPageIndex >= PageCount);   //是否尾页
        }
        /// <summary>
        /// 是否陈列出来
        /// </summary>
        /// <param name="isdis">true:隐藏</param>
        /// <returns></returns>
        protected string GetDisplay(bool isdis)
        {
            return (isdis) ? "Display:none;" : "";
        }
        /// <summary>
        /// 获取页面+参数+分页索引
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <returns></returns>
        protected string GetUrl_PageIndex(object pageIndex)
        { 
            StringBuilder sb = new StringBuilder();
            sb.Append("\"").Append(PageURL).Append("?").Append(URLQuery).Append("Page=").Append(pageIndex).Append("\"");
            return sb.ToString();
        }
        #endregion

        #region 抽象方法
        /// <summary>
        /// 获取分页后的UI界面
        /// </summary>
        /// <returns>html代码</returns>
        public abstract string GetSchema();
        #endregion
    }
}